# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=poco
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.11.6
pkgrel=2
pkgdesc="POrtable COmponents C++ Libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://pocoproject.org/"
license=('spdx:BSL-1.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-apr"
         "${MINGW_PACKAGE_PREFIX}-apr-util"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://pocoproject.org/releases/${_realname}-${pkgver}/${_realname}-${pkgver}-all.tar.gz
        004-format-string-using-ostringstream.patch
        005-dont-compare-signed-unsigned.patch
        007-mingw-wide-character-startup-module.patch
        008-Add-Mingw32-W64-configuration.patch
        010-remove-warnings.patch
        011-cmake-mingw.patch
        012-fix-mingw-redefinition.patch
        100-fix-mingw-config.patch)
sha256sums=('f1f8004a3d304861d78b3341458d298d37c7d3d84938fb019e0ce3002dde951d'
            '94bf2a9ddfa00d0ec804283d6fd4cd0c13c9001e0beb44f16ac5250b5b90845e'
            'ec37e78bd3f57403b9e4d69a4ad016c451c8552c88b4e3b319e61b015190206c'
            '8c89571920fb5d0ecaae1b890854335fb5d310243baa4723233fe46544cf4f1b'
            '61979c75b5577b9b0bf3fb96045b3c299a38c5dfc5048895ade73400831eb975'
            '542abadbead21f4539a3aad118bfb08f63e531a70db6f17bc08da1b171c4563e'
            'a566cdc7cbeedb1666f108d0e339b9c3d7d3b1fb8e9b99c1c2f2566548e6ffbf'
            '6ba1dcf47a6976496467965bb9eafd1453ae373d0fc52997c1c32ff799372ad2'
            '861cc9dfae04c1843ac7d1010eb7f31596b123624bcd3cee5dbef0ac4394f9b1')

case ${MSYSTEM} in
  CLANG*)
    # work around lack of windmc in llvm
    makedepends+=("binutils")
      ;;
esac

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}-all
  patch -p1 -i ${srcdir}/004-format-string-using-ostringstream.patch
  patch -p1 -i ${srcdir}/005-dont-compare-signed-unsigned.patch
  patch -p1 -i ${srcdir}/007-mingw-wide-character-startup-module.patch
  patch -p1 -i ${srcdir}/008-Add-Mingw32-W64-configuration.patch
  patch -p1 -i ${srcdir}/010-remove-warnings.patch
  patch -p1 -i ${srcdir}/011-cmake-mingw.patch
  patch -p1 -i ${srcdir}/012-fix-mingw-redefinition.patch
  patch -p1 -i ${srcdir}/100-fix-mingw-config.patch
}

build() {
  local _config=
  case "${CARCH}" in
    i686)
      _config=MinGW
    ;;

    x86_64)
      _config=MinGW32-W64
    ;;
  esac

  case {$MSYSTEM} in
    CLANG*)
      makedepends+=("binutils")
    ;;
  esac

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -DCMAKE_C_FLAGS="${CPPFLAGS} -DPCRE_STATIC ${CFLAGS}" \
    -DCMAKE_CXX_FLAGS="${CPPFLAGS} -DPCRE_STATIC ${CXXFLAGS}" \
    -DBUILD_SHARED_LIBS=OFF \
    -DPOCO_UNBUNDLED=ON \
    -DENABLE_MONGODB=OFF \
    -DENABLE_DATA_ODBC=OFF \
    ../${_realname}-${pkgver}-all

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -DCMAKE_C_FLAGS="${CPPFLAGS} ${CFLAGS}" \
    -DCMAKE_CXX_FLAGS="${CPPFLAGS} ${CXXFLAGS}" \
    -DBUILD_SHARED_LIBS=ON \
    -DPOCO_UNBUNDLED=ON \
    -DENABLE_MONGODB=OFF \
    -DENABLE_DATA_ODBC=OFF \
    ../${_realname}-${pkgver}-all

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  install -Dm644 "${srcdir}/${_realname}-${pkgver}-all/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
